/**
 *
 * Copyright (c) 2001-2019 泛微软件.
 * 泛微协同商务系统,版权所有.
 *
 */

package com.api.browser.service.impl;

import com.api.browser.bean.ListHeadBean;
import com.api.browser.service.BrowserService;
import com.api.browser.util.BoolAttr;
import com.api.browser.util.BrowserBaseUtil;
import com.api.browser.util.BrowserConstant;
import com.api.browser.util.BrowserDataType;
import com.engine.fnaMulDimensions.util.AccountInfoComInfo;
import org.apache.commons.lang.StringEscapeUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.systeminfo.SystemEnv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * （多）维度成员浏览按钮
 * @author xuf
 * @date 2019-2-21
 *
 */
public class MultiMemberBrowserService extends BrowserService{

    /**
     * 获取浏览按钮数据
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> getBrowserData(Map<String, Object> params)throws Exception {
        
        Map<String, Object> apidatas = new HashMap<String, Object>();
        
        if(user == null){
            apidatas.put(BrowserConstant.BROWSER_RESULT_DATA, null);
            return apidatas;
        }
        
        
        RecordSet rs = new RecordSet();
        
        String queryName = Util.null2String(params.get("name"));
        String accountId = Util.null2String(params.get("accountId"));
        String dimensionId = Util.null2String(params.get("dimensionId"));
        
        AccountInfoComInfo accountInfoComInfo = new AccountInfoComInfo();
        String numberCode = accountInfoComInfo.getNumberCode(accountId);
        String tableName = "FnaDimensionMember_"+numberCode;
        
        List<Map<String,String>> dataList = new ArrayList<Map<String,String>>();
        
        StringBuffer buffer = new StringBuffer();
        buffer.append(" select a.id,a.fkName from ").append(tableName).append(" a ");
        buffer.append(" where a.dimensionId = '").append(dimensionId).append("'");
        if(!"".equals(queryName)){
            buffer.append(" and a.fkName like '%").append(StringEscapeUtils.escapeSql(queryName)).append("%' "); 
        }
        buffer.append(" order by a.displayOrder ");
        
        rs.executeQuery(buffer.toString());
        while(rs.next()){
            
            String id = Util.null2String(rs.getString("id"));
            String fkName = Util.null2String(rs.getString("fkName"));
            
            Map<String,String> map = new HashMap<String,String>();
            map.put("id", id);
            map.put("fkName", fkName);
            
            dataList.add(map);
        }
        
        List<ListHeadBean> tableHeadColumns =  new ArrayList<ListHeadBean>();
        tableHeadColumns.add(new ListHeadBean("id",BoolAttr.TRUE));
        tableHeadColumns.add(new ListHeadBean("fkName",SystemEnv.getHtmlLabelName(195,user.getLanguage()),1).setIsInputCol(BoolAttr.TRUE));
        
        apidatas.put(BrowserConstant.BROWSER_RESULT_COLUMN, tableHeadColumns);
        apidatas.put(BrowserConstant.BROWSER_RESULT_DATA, dataList);
        apidatas.put(BrowserConstant.BROWSER_RESULT_TYPE, BrowserDataType.LIST_ALL_DATA.getTypeid());
        
        return apidatas;
    }

    /**
     * 获取穿梭浏览框选中列表数据
     * @param params
     * @return 
     */
    @Override
    public Map<String, Object> getMultBrowserDestData(Map<String, Object> params) throws Exception {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        
        String selectids  = Util.null2String(params.get(BrowserConstant.BROWSER_MULT_DEST_SELECTIDS));
        String accountId = Util.null2String(params.get("accountId"));
        AccountInfoComInfo accountInfoComInfo = new AccountInfoComInfo();
        String numberCode = accountInfoComInfo.getNumberCode(accountId);
        String tableName = "FnaDimensionMember_"+numberCode;
        new BaseBean().writeLog("MultiMemberBrowserService_selectids:"+selectids);
        List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
        
        String[] selectIdArray = selectids.split(",");
        String newStr = "";
        for(int i = 0; i < selectIdArray.length; i++){
            newStr += "'"+selectIdArray[i]+"'"+",";
        }
        newStr = newStr.substring(0, newStr.length()-1);
        
        StringBuffer buffer = new StringBuffer();
        buffer.append(" select a.id,a.fkName from "+tableName+" a ");
        buffer.append(" where 1=1 ");
        buffer.append(" and a.id in (").append(newStr).append(")");
        
        RecordSet rs  = new RecordSet();
        rs.executeQuery(buffer.toString());
        while(rs.next()){
            Map<String,Object> item = new HashMap<String,Object>();
            item.put("id",Util.null2String(rs.getString("id")));
            item.put("fkName", Util.null2String(rs.getString("fkName")));
            datas.add(item);
        }
        List<ListHeadBean> tableHeadColumns =  new ArrayList<ListHeadBean>();
        tableHeadColumns.add(new ListHeadBean("id",BoolAttr.TRUE).setIsPrimarykey(BoolAttr.TRUE));
        tableHeadColumns.add(new ListHeadBean("fkName","",1,BoolAttr.TRUE));
        
        apidatas.put(BrowserConstant.BROWSER_RESULT_COLUMN, tableHeadColumns);
        apidatas.put(BrowserConstant.BROWSER_RESULT_DATA, BrowserBaseUtil.sortDatas(datas,selectids,"id"));
        apidatas.put(BrowserConstant.BROWSER_RESULT_TYPE, BrowserDataType.LIST_ALL_DATA.getTypeid());
        
        return apidatas;
    }
}
